
using MySqlConnector;

namespace Front._1127;

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void Login_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
        
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            
            string account = textBox1.Text?.Trim();
            string password = textBox2.Text;
            
            if (string.IsNullOrEmpty(account))
            {
                MessageBox.Show("请输入账号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox1.Focus();
                return;
            }
            
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox2.Focus();
                return;
            }

           
            string pwd = MD5Helper.GetMD5(password);
            
           
            string connectionString = "server=localhost;port=3306;database=book;user=root;password=liushuqi123456.;charset=utf8mb4;AllowPublicKeyRetrieval=True;";
            
            using var connection = new MySqlConnection(connectionString);
            connection.Open();
            
            
            string sql = "SELECT * FROM admin_info WHERE admin_account = @account AND admin_password = @pwd";
            using var command = new MySqlCommand(sql, connection);
            
            
            command.Parameters.AddWithValue("@account", account);
            command.Parameters.AddWithValue("@pwd", pwd);
            
            using var reader = command.ExecuteReader();
            if (reader.HasRows)
            {
               
                MainForm mainForm = new MainForm();
                mainForm.Show();
                this.Hide();
                
                
                mainForm.FormClosed += (s, args) => this.Close();
            }
            else
            {
                
                MessageBox.Show("账号或密码错误", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.textBox2.Text = "";
                this.textBox2.Focus();
            }
        }
        catch (MySqlException ex)
        {
           
            MessageBox.Show($"数据库错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
            
            MessageBox.Show($"登录失败：{ex.Message}\n错误类型：{ex.GetType().Name}\n堆栈跟踪：{ex.StackTrace}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}